//
//  ViewController.m
//  OCLockDemo
//
//  Created by lianglibao on 2018/12/16.
//  Copyright © 2018年 lianglibao. All rights reserved.
//

#import "ViewController.h"
#import "BaseDemo.h"
#import "OSSpinLockDemo.h"
#import "OSSpinLockDemo2.h"
#import "OSUnfairLockDemo.h"
#import "MutexDemo.h"
#import "MutexDemo2.h"
#import "MutexDemo3.h"
#import "NSLockDemo.h"
#import "NSConditionDemo.h"
#import "NSConditionLockDemo.h"
#import "SerialQueueDemo.h"
#import "SemaphoreDemo.h"
#import "SynchronizedDemo.h"

/// 读写锁
#import "PthreadRwlockDemo.h"
#import "BarrierRwlockDemo.h"

/// 测试线程保活
#import "TwoViewController.h"

#define SemaphoreBegin \
static dispatch_semaphore_t semaphore; \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
    semaphore = dispatch_semaphore_create(1); \
}); \
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

#define SemaphoreEnd \
dispatch_semaphore_signal(semaphore);

@interface ViewController ()
@property (strong, nonatomic) BaseDemo *demo;

@property (strong, nonatomic) NSThread *thread;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSStringFromClass(self.class);
    
//    BaseDemo *demo = [[BaseDemo alloc] init];
//    [demo ticketTest];
//    [demo moneyTest];
//    [demo otherTest];
    
//    [PthreadRwlockDemo pthreadRwlockTest];
	
    [self setupSubviews];
    
    // test Arr for not contain anyone object..
    NSArray *arr = @[@"1", @"2", @"3"];
    NSInteger index = [arr indexOfObject:@"5"];
    NSLog(@"+++++++ %ld", index);
}

- (void)setupSubviews {
    [self.view addSubview:({
        UIButton *jumpBtn = [UIButton new];
        [jumpBtn setTitle:@"GO TO NEXT" forState:UIControlStateNormal];
        [jumpBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [jumpBtn addTarget:self action:@selector(jumpToNext) forControlEvents:UIControlEventTouchUpInside];
        jumpBtn.backgroundColor = [UIColor lightGrayColor];
        [jumpBtn sizeToFit];
        jumpBtn.center = self.view.center;
        jumpBtn;
    })];
}

- (void)jumpToNext {
    TwoViewController *twoVC = [TwoViewController new];
    [self.navigationController pushViewController:twoVC animated:YES];
}

- (void)test
{
    NSLog(@"2");
}

- (void)test1
{
    SemaphoreBegin;
    
    // .....
    
    SemaphoreEnd;
}

- (void)test2
{
    SemaphoreBegin;
    
    // .....
    
    SemaphoreEnd;
}

- (void)test3
{
    SemaphoreBegin;
    
    // .....
    
    SemaphoreEnd;
}

@end
